package disruptor;

import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.EventTranslatorVararg;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;

import java.util.concurrent.Executors;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-20 19:51
 **/
public class DisruptorTest03 {


    public static void main(String[] args) {

        Disruptor<DisruptorEvent> disruptor = new Disruptor<>(DisruptorEvent::new, 1024, Executors.defaultThreadFactory());
        disruptor.handleEventsWith((event, sequence, endOfBatch) -> System.out.println("Event: " + event));
        disruptor.start();
        // Get the ring buffer from the Disruptor to be used for publishing.
        RingBuffer<DisruptorEvent> ringBuffer = disruptor.getRingBuffer();

//        EventTranslator
        ringBuffer.publishEvent((event, sequence) -> event.set(10000L));
//        EventTranslatorOneArg
        ringBuffer.publishEvent((event, sequence, l) -> event.set(l), 10000L);
//        EventTranslatorTwoArg
        ringBuffer.publishEvent((event, sequence, l1, l2) -> event.set(l1 + l2),10000L, 20000L);
//        EventTranslatorThreeArg
        ringBuffer.publishEvent((event, sequence, l1, l2,l3) -> event.set(l1 + l2 + l3),10000L, 20000L,3000L);
//        EventTranslatorThreeArg
        ringBuffer.publishEvent((event, sequence, l1, l2,l3) -> {
            event.set(l1 + l2 + l3);
        },10000L, 20000L,3000L);

        disruptor.shutdown();
    }
}
